package org.jypj.dev.service.impl;


import org.jypj.dev.dao.IThingProblemDao;
import org.jypj.dev.model.ThingProblem;
import org.jypj.dev.service.IThingProblemService;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("thingProblemService")
public class ThingProblemServiceImpl implements IThingProblemService {

    @Resource
    private IThingProblemDao thingProblemDao;

    /**
     * 保存
     * 字段为空的不存防止覆盖存在默认值的字段
     *
     * @param thingProblem
     * @return 保存后的对象包括ID
     */
    public int saveThingProblemByField(ThingProblem thingProblem) {
        if (StringUtil.isEmpty(thingProblem.getId())) {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            thingProblem.setId(uuid);
        }
        return thingProblemDao.saveThingProblemByField(thingProblem);
    }

    /**
     * 保存
     * 所有字段全都保存
     *
     * @param thingProblem
     * @return 保存后的对象包括ID
     */
    public int saveThingProblem(ThingProblem thingProblem) {
        if (StringUtil.isEmpty(thingProblem.getId())) {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            thingProblem.setId(uuid);
        }
        return thingProblemDao.saveThingProblem(thingProblem);
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteThingProblemById(String id) {

        return thingProblemDao.deleteThingProblemById(id);
    }

    /**
     * 根据对象删除
     *
     * @param id 主键ID
     * @return
     */
    public int deleteThingProblemByObject(ThingProblem thingProblem) {

        return thingProblemDao.deleteThingProblemByObject(thingProblem);
    }

    /**
     * 更新
     * 只更新值不为空的字段
     *
     * @param thingProblem
     * @return 保存后的对象包括ID
     */
    public int updateThingProblemByField(ThingProblem thingProblem) {

        return thingProblemDao.updateThingProblemByField(thingProblem);
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param thingProblem
     * @return 保存后的对象包括ID
     */
    public int updateThingProblem(ThingProblem thingProblem) {

        return thingProblemDao.updateThingProblem(thingProblem);
    }

    /**
     * 按ID查询
     *
     * @return ThingProblem
     * @parm id 主键ID
     */
    public ThingProblem selectThingProblemById(String id) {

        return thingProblemDao.selectThingProblemById(id);
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<ThingProblem>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<ThingProblem> thingProblems = thingProblemDao.selectOnePageByMap(page, map);
        if (thingProblems != null && thingProblems.size() > 0) {
            page.setResult(thingProblems);
        } else {
            page.setResult(new ArrayList<ThingProblem>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page         分页对象
     * @param thingProblem 查询条件
     * @return Page
     */
    public Page selectOnePageByThingProblem(Page page, ThingProblem thingProblem) {
        List<ThingProblem> thingProblems = thingProblemDao.selectOnePageByThingProblem(page, thingProblem);
        if (thingProblems != null && thingProblems.size() > 0) {
            page.setResult(thingProblems);
        } else {
            page.setResult(new ArrayList<ThingProblem>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<ThingProblem>
     */
    public List<ThingProblem> selectAllByMap(Map<String, Object> map) {
        return thingProblemDao.selectAllByMap(map);
    }


    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<ThingProblem>
     */
    public List<ThingProblem> selectAllByThingProblem(ThingProblem thingProblem) {

        return thingProblemDao.selectAllByThingProblem(thingProblem);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return ThingProblem
     */
    public ThingProblem selectObjectByMap(Map<String, Object> map) {

        return thingProblemDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return ThingProblem
     */
    public ThingProblem selectObjectByThingProblem(ThingProblem thingProblem) {

        return thingProblemDao.selectObjectByThingProblem(thingProblem);
    }
}